/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package stis.statpro.ibs.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import stis.statpro.ibs.datasource.Datasource;
import stis.statpro.ibs.model.Kabupaten;

/**
 *
 * @author habib <imamhabibpamungkas@gmail.com>
 */
public class KabupatenDao {

    private List<Kabupaten> listKab;

    public List<Kabupaten> getALl(int id_prov) throws SQLException {
        listKab = new ArrayList<>();
        Connection c = Datasource.getConnection();
        PreparedStatement p = c.prepareStatement("SELECT * from kabupaten where id_prov=?;");
        p.setInt(1, id_prov);
        ResultSet rs = p.executeQuery();
        while (rs.next()) {
            Kabupaten kab = new Kabupaten();
            kab.setIdKab(rs.getInt("id"));
            kab.setIdProv(rs.getInt("id_prov"));
            kab.setNama(rs.getString("nama"));
            listKab.add(kab);
        }
        p.close();
        return listKab;
    }

    public List<Kabupaten> getById(int nama) throws SQLException {
        Connection c = Datasource.getConnection();
        listKab = new ArrayList<>();
        PreparedStatement p = c.prepareStatement("SELECT * from Kabupaten where id_prov=?;");
        p.setInt(1, nama);
        ResultSet rs = p.executeQuery();
        while (rs.next()) {
            Kabupaten kab = new Kabupaten();
            kab.setIdKab(rs.getInt("id"));
            kab.setIdProv(rs.getInt("id_prov"));
            kab.setNama(rs.getString("nama"));
            listKab.add(kab);
        }
        p.close();
        if (listKab == null) {
            return null;
        } else {
            return listKab;
        }
    }
}
